﻿using Common.Untils;
using HandyControl.Controls;

using Prism.Ioc;
using System;
using System.Security.Principal;
using System.Threading.Tasks;
using System.Windows;
using Tryit.ViewModels;
using Tryit.Views;
using MessageBox = HandyControl.Controls.MessageBox;
using Window = System.Windows.Window;

namespace Tryit
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App
    {
        protected override Window CreateShell()
        {
            // 注册全局未处理异常处理器
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
            return Container.Resolve<HomeWindow>();
        }

        private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
        {
            Exception ex = (Exception)e.Exception;
            Log.WriteLog(ex.StackTrace, ex.Message);
        }

        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception ex = (Exception)e.ExceptionObject;
            Log.WriteLog(ex.StackTrace, ex.Message);
        }

        protected override void InitializeShell(Window shell)
        {
            if (!IsAdministrator())
            {
                //MessageBox.Show("请用管理员模式打开程序！","Warning",MessageBoxButton.OK,MessageBoxImage.Warning);
                //Application.Current.Shutdown();
            }
            else base.InitializeShell(shell);

        }
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.RegisterForNavigation<RecordSearch>();
            containerRegistry.RegisterForNavigation<Settings>();
            containerRegistry.RegisterForNavigation<SummonerInfo>();
            containerRegistry.RegisterForNavigation<Error>();
        }
        public bool IsAdministrator()
        {
            WindowsIdentity identity = WindowsIdentity.GetCurrent();
            WindowsPrincipal principal = new WindowsPrincipal(identity);
            return principal.IsInRole(WindowsBuiltInRole.Administrator);
        }
    }
}
